package com.gongye.courseselectionsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gongye.courseselectionsystem.common.Check;
import com.gongye.courseselectionsystem.constants.ResultCodeEnum;
import com.gongye.courseselectionsystem.mapper.ChooseCourseMapper;
import com.gongye.courseselectionsystem.mapper.CourseMapper;
import com.gongye.courseselectionsystem.pojo.po.ChooseCourse;
import com.gongye.courseselectionsystem.pojo.po.Student;
import com.gongye.courseselectionsystem.pojo.vo.ChooseCourseStudent;
import com.gongye.courseselectionsystem.pojo.vo.CourseVo;
import com.gongye.courseselectionsystem.pojo.vo.ResultList;
import com.gongye.courseselectionsystem.service.QueryCourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author baorongchuan
 */
@Slf4j
@Service
public class QueryCourseServiceImpl implements QueryCourseService {

    @Resource
    private CourseMapper courseMapper;
    @Resource
    private RedisTemplate<String, CourseVo> redisTemplate;
    @Resource
    private ChooseCourseMapper chooseCourseMapper;
    @Resource
    private Check check;

    /**
     * 查询出所有课程对应的可以选择的老师
     *
     * @return 返回所有课程对应的可以选择的老师集合
     */
    @Override
    public List<CourseVo> getCourseVo() {
        List<CourseVo> courseVo = redisTemplate.opsForList().range("courseVo", 0, -1);
        assert courseVo != null;
        if (courseVo.size() != 0) {
            return courseVo;
        }
        List<CourseVo> courseVos2 = courseMapper.optionalCourseVo();
        redisTemplate.opsForList().rightPushAll("courseVo", courseVos2);
        return courseVos2;
    }

    /**
     * 返回所有已选课的学生信息
     */
    @Override
    public List<ChooseCourse> queryAll() {
        return chooseCourseMapper.selectList(new QueryWrapper<ChooseCourse>().orderByAsc("courseNumber"));
    }

    @Override
    public List<ChooseCourseStudent> queryChooseCourseStudent() {
        return chooseCourseMapper.selectChooseCourse();
    }

    /**
     * 根据学生学号查询自己的选课信息
     *
     * @param studentNumber 学生的标号
     * @return 返回学生选课详情
     */
    @Override
    public ResultList queryChooseById(Integer studentNumber) {
        // 根据学号查询学生是否存在
        Student studentById = check.getStudentById(studentNumber);
        if (studentById == null) {
            log.info("您输入的学号不存在");
            return new ResultList(ResultCodeEnum.FAILED, "您输入的学号不存在");
        }
        // 根据学生学号查询自己的选课信息
        LambdaQueryWrapper<ChooseCourse> eq = new LambdaQueryWrapper<ChooseCourse>().eq(ChooseCourse::getStudentNumber, studentNumber);
        List<ChooseCourse> chooseCourses = chooseCourseMapper.selectList(eq);
        if (chooseCourses.size() == 0) {
            return new ResultList(ResultCodeEnum.FAILED, "学号为'" + studentNumber + "'的学生没有选课");
        }
        return new ResultList(ResultCodeEnum.SUCCESS, chooseCourses);
    }
}
